function CheckSession() {
    //TODO 判断session过期
    if( GetSessionKeyVale('username') == ''){
        layui.layer.msg('登录超时，请重新登录', {
            icon: 2,
            time: 1000
        });
        setTimeout(function () {
            parent.location.href = "/login.html"
        }, 1000);
    }
}


function toFloatOrInt(num) {
    //TODO 保留整数或小数后两位
    if(num.split('.')[1] == 00 ){
        num = parseInt(num);
    }else{
        num = num;
    }
    return num;
}



function renderPayType(name) {
    //TODO 渲染收费方式
    $.ajax(
        {
            type: "post",
            async: false,
            url: "/Manage/getSysPayType",
            data:{
                org_id:GetSessionKeyVale('org_id'),
                status:0
            },
            success:function (data) {
                $("#"+name).html('');
                var paytype = data.data;
                var base = $("<option>").val("").text('');
                $("#"+name).append(base);
                for(var i=0; i<paytype.length; i++)
                {
                    var option = $("<option>").val(paytype[i].id).text(paytype[i].payType).attr('isbox',paytype[i].isbox);
                    $("#"+name).append(option);
                    layui.form.render('select', 'qte');
                }
            }
        }
    );
}


function renderBillType(name) {
    //TODO 渲染票据类型
    $.ajax(
        {
            type: "post",
            async: false,
            url: "/notes/listSysBillType",
            data:{
                status:0
            },
            success:function (data) {
                $("#"+name).html('');
                var billtype = data.data;
                var base = $("<option>").val("").text('请选择票据类型');
                $("#"+name).append(base);
                for(var i=0; i<billtype.length; i++)
                {
                    var option = $("<option>").val(billtype[i].id).text(billtype[i].billTypeName);
                    $("#"+name).append(option);
                    layui.form.render('select', 'sel_bill_type');
                }
            }
        }
    );
}



function renderUserName(name) {
    //TODO 渲染用户
    $.ajax(
        {
            type: "post",
            async: false,
            url: "/Manage/listUser",
            data:{
                status:0
            },
            success:function (data) {
                $("#"+name).html('');
                var user = eval('(' + data.data + ')');
                var base = $("<option>").val("").text('请选择用户');
                $("#"+name).append(base);
                for(var i=0; i<user.length; i++)
                {
                    var option = $("<option>").val(user[i].uId).text(user[i].name);
                    $("#"+name).append(option);
                    layui.form.render('select', 'sel_user');
                }
            }
        }
    );
}



function renderIsWxBox() {
    //TODO 检查微信支付是否使用扫码盒子
    var isbox = '';
    var arr={};
    $.ajax(
        {
            type: "post",
            async: false,
            url: "/Manage/getSysPayType",
            data:{
                org_id:GetSessionKeyVale('org_id'),
                status:0
            },
            success:function (data) {
                var paytype = data.data;
                for(var i=0; i<paytype.length; i++)
                {
                   if( paytype[i].payType == '微信支付'){
                       isbox = paytype[i].isbox
                       arr={
                           "isbox":paytype[i].isbox,
                           "id":paytype[i].id
                       };
                   }
                }
            }
        }
    );
    return arr
}


function renderIsAliBox() {
    //TODO 检查支付宝支付是否使用扫码盒子
    var isbox = '';
    var arr={};
    $.ajax(
        {
            type: "post",
            async: false,
            url: "/Manage/getSysPayType",
            data:{
                org_id:GetSessionKeyVale('org_id'),
                status:0
            },
            success:function (data) {
                var paytype = data.data;
                for(var i=0; i<paytype.length; i++)
                {
                    if( paytype[i].payType == '支付宝'){
                        isbox = paytype[i].isbox
                        arr={
                            "isbox":paytype[i].isbox,
                            "id":paytype[i].id
                        };
                    }
                }
            }
        }
    );
    return arr
}



function renderCommunityUser(name) {
    //TODO 渲染用户片区
    $.ajax(
        {
            type: "post",
            async: false,
            url: "/Manage/getSysUsercom",
            success:function (data) {
                $("#"+name).html('');
                var community = data.data;
                // var base = $("<option>").val("").text('请选择小区');
                // $("#"+name).append(base);
                for(var i=0; i<community.length; i++)
                {
                    var option = $("<option>").val(community[i].id).text(community[i].communityName);
                    $("#"+name).append(option);
                    layui.form.render('select', 'sel_community');
                }
            }
        }
    );
}


function renderOrg(name) {
    //TODO 渲染机构
    $.ajax(
        {
            type: "post",
            async: false,
            url: "/Manage/getSysOrg",
            data: {
                status:0
            },
            success:function (data) {
                $("#"+name).html('');
                var orgdata = data.data;
                var base = $("<option>").val("").text('全部');
                $("#"+name).append(base);
                for(var i=0; i<orgdata.length; i++)
                {
                    var option = $("<option>").val(orgdata[i].orgId).text(orgdata[i].orgName);
                    $("#"+name).append(option);
                    layui.form.render('select', 'sel_org');
                }
            }
        }
    );
}

function renderCommunity(name) {
    //TODO 渲染小区
    $.ajax(
        {
            type: "post",
            async: false,
            url: "/Manage/listManageCommunity",
            data:{
                org_id:GetSessionKeyVale('org_id'),
                status:0
            },
            success:function (data) {
                $("#"+name).html('');
                var community = data;
                var base = $("<option>").val("").text('全部');
                $("#"+name).append(base);
                for(var i=0; i<community.length; i++)
                {
                    var option = $("<option>").val(community[i].id).text(community[i].communityName);
                    $("#"+name).append(option);
                    layui.form.render('select', 'sel_community');
                }
            }
        }
    );
}

function renderAcessView(name) {
    //TODO 渲染门禁区域
    $.ajax(
        {
            type: "post",
            async: false,
            url: "/guard/listSysAccessView",
            data:{
                status:0,
                orgId:GetSessionKeyVale('org_id')
            },
            success:function (data) {
                $("#"+name).html('');
                var AccessView = data.data;
                var base = $("<option>").val("").text('全部');
                $("#"+name).append(base);
                for(var i=0; i<AccessView.length; i++)
                {
                    var option = $("<option>").val(AccessView[i].id).text(AccessView[i].viewName);
                    $("#"+name).append(option);
                    layui.form.render('select', 'sel_access_view');
                }
            }
        }
    );
}


function renderServeType(name) {
    //TODO 渲染服务类型
    $.ajax(
        {
            type: "post",
            async: false,
            url: "/serve/listSysServeType",
            data:{
                status:0
            },
            success:function (data) {
                $("#"+name).html('');
                var servetype = data.data;
                var base = $("<option>").val("").text('请选择类型');
                $("#"+name).append(base);
                for(var i=0; i<servetype.length; i++)
                {
                    var option = $("<option>").val(servetype[i].id).text(servetype[i].serveType);
                    $("#"+name).append(option);
                    layui.form.render('select', 'sel_serve_type');
                }
            }
        }
    );
}


function renderItemType(name) {
    //TODO 渲染项目类型
    $.ajax(
        {
            type: "post",
            async: false,
            url: "/Manage/listSysItemType",
            data:{
                status:0
            },
            success:function (data) {
                $("#"+name).html('');
                var data = data.data;
                var base = $("<option>").val("").text('请选择类型');
                $("#"+name).append(base);
                for(var i=0; i<data.length; i++)
                {
                    var option = $("<option>").val(data[i].id).text(data[i].itemTypeName);
                    $("#"+name).append(option);
                    layui.form.render('select', 'sel_item_type');
                }
            }
        }
    );
}


function renderPakingArea(name) {
    //TODO 渲染车位区域
    $.ajax(
        {
            type: "post",
            async: false,
            url: "/Manage2/listSysPakingarea",
            data:{
                status:0
            },
            success:function (data) {
                $("#"+name).html('');
                var data = data.data;
                var base = $("<option>").val("").text('请选择区域');
                $("#"+name).append(base);
                for(var i=0; i<data.length; i++)
                {
                    var option = $("<option>").val(data[i].id).text(data[i].pakingareaName);
                    $("#"+name).append(option);
                    layui.form.render('select', 'sel_pakingarea');
                }
            }
        }
    );
}



function disItemType(con,tar){
    //TODO 筛选项目类型费用
    layui.form.on('select(disItemType)', function () {
        $.ajax({
            type: "post",
            async: false,
            url: "/Manage/listSysItem",
            data: {
                item_type_id:$(con).val(),
                status:0
            },
            success:function (data) {
                if(data.status == 200){
                    $(tar).html('');
                    var itemtype = data.data;
                    var base = $("<option>").val("").text('请选择项目');
                    $(tar).append(base);
                    for(var i=0; i<itemtype.length; i++)
                    {
                        var option = $("<option>").val(itemtype[i].id).text(itemtype[i].itemName);
                        $(tar).append(option);
                        layui.form.render('select', 'sel_item');
                    }
                }
            }
        });
    });
}


function renderBuilding(community_id,building_id){
    //TODO 加载楼栋数据
    layui.form.on('select(community)', function () {
        $.ajax(
            {
                type: "post",
                async: false,
                url: "/Manage/listManageBuilding",
                data: {
                    community_id:$("#"+community_id).val(),
                    status:0
                },
                success:function (data) {
                    if(data.length == 0){
                        $("#"+building_id).html('');
                        var base = $("<option>").val("").text('无');
                        $("#"+building_id).append(base);
                        layui.form.render('select', 'sel_building');
                    }else{
                        var building = data;
                        $("#"+building_id).html('');
                        var base = $("<option>").val("").text('全部');
                        $("#"+building_id).append(base);
                        for(var i=0; i<building.length; i++)
                        {
                            var option = $("<option>").val(building[i].id).text(building[i].buildingName);
                            $("#"+building_id).append(option);
                            layui.form.render('select', 'sel_building');
                        }
                        $("#"+community_id).attr('cookie_community_id',$("#"+community_id).val())
                    }
                }
            }
        );
    });
    layui.form.on('select(_community)', function () {
        $.ajax(
            {
                type: "post",
                async: false,
                url: "/Manage/listManageBuilding",
                data: {
                    community_id:$("#"+community_id).val(),
                    status:0
                },
                success:function (data) {
                    if(data.length == 0){
                        $("#"+building_id).html('');
                        var base = $("<option>").val("").text('无');
                        $("#"+building_id).append(base);
                        layui.form.render('select', 'sel_building');
                    }else{
                        var building = data;
                        $("#"+building_id).html('');
                        var base = $("<option>").val("").text('全部');
                        $("#"+building_id).append(base);
                        for(var i=0; i<building.length; i++)
                        {
                            var option = $("<option>").val(building[i].id).text(building[i].buildingName);
                            $("#"+building_id).append(option);
                            layui.form.render('select', 'sel_building');
                        }
                        $("#"+community_id).attr('cookie_community_id',$("#"+community_id).val())
                    }
                }
            }
        );
    });
}



function renderOrgCommunity(org_id,community_id){
    //TODO 加载机构小区
    layui.form.on('select(org)', function () {
        $.ajax(
            {
                type: "post",
                async: false,
                url: "/Manage/listManageCommunity",
                data: {
                    org_id:$("#"+org_id).val(),
                    status:0
                },
                success:function (data) {
                    if(data.length == 0){
                        $("#"+community_id).html('');
                        var base = $("<option>").val("").text('无');
                        $("#"+community_id).append(base);
                        layui.form.render('select', 'sel_community');
                    }else{
                        var community = data;
                        $("#"+community_id).html('');
                        var base = $("<option>").val("").text('全部');
                        $("#"+community_id).append(base);
                        for(var i=0; i<community.length; i++)
                        {
                            var option = $("<option>").val(community[i].id).text(community[i].communityName);
                            $("#"+community_id).append(option);
                            layui.form.render('select', 'sel_community');
                        }
                        // $("#"+org_id).attr('cookie_community_id',$("#"+org_id).val())
                    }
                }
            }
        );
    });
}



function renderBuildingLoad(community_id,building_id){
    //TODO 加载楼栋数据
    $.ajax(
        {
            type: "post",
            async: false,
            url: "/Manage/listManageBuilding",
            data: {
                community_id:$("#"+community_id).val(),
                status:0
            },
            success:function (data) {
                if(data.length == 0){
                    $("#"+building_id).html('');
                    var base = $("<option>").val("").text('无');
                    $("#"+building_id).append(base);
                    layui.form.render('select', 'sel_building');
                }else{
                    var building = data;
                    $("#"+building_id).html('');
                    var base = $("<option>").val("").text('全部');
                    $("#"+building_id).append(base);
                    for(var i=0; i<building.length; i++)
                    {
                        var option = $("<option>").val(building[i].id).text(building[i].buildingName);
                        $("#"+building_id).append(option);
                        layui.form.render('select', 'sel_building');
                    }
                }
            }
        }
    );
}


function calmonth(startday, endday, loc){
    //TODO 计算月份数量
    var start = startday.split('-')[2];
    if( start == '01'){
        var months =  datemonth(startday,endday);
        $(loc).val(months);
    }else{
        var months =  datemonth(startday,endday) - 1;
        $(loc).val(months);
    }
}


function calmonthReturnMonth(startday, endday){
    //TODO 计算月份数量(返回值)
    var start = startday.split('-')[2];
    var months = 0;
    if( start == '01'){
        months =  datemonth(startday,endday);
    }else{
        months =  datemonth(startday,endday) - 1;
    }
    return months
}



function renderItem(name) {
    //TODO 渲染收费项目
    $.ajax(
        {
            type: "post",
            async: false,
            url: "/Manage/getSysItem",
            data:{
                status:0
            },
            success:function (data) {
                if(data.status == 200){
                    $("#"+name).html('');
                    var item = data.data;
                    var base = $("<option>").val("").text('请选择项目');
                    $("#"+name).append(base);
                    for(var i=0; i<item.length; i++)
                    {
                        var option = $("<option>").val(item[i].id).text(item[i].itemName);
                        $("#"+name).append(option);
                        layui.form.render('select', 'sel_item');
                    }
                }
            }
        }
    );
}



function renderTemItem(name) {
    //TODO 渲染临时收费项目
    $.ajax(
        {
            type: "post",
            async: false,
            url: "/Manage2/listSysTemItem",
            data:{
                status:0
            },
            success:function (data) {
                if(data.status == 200){
                    $("#"+name).html('');
                    var item = data.data;
                    var base = $("<option>").val("").text('请选择项目');
                    $("#"+name).append(base);
                    for(var i=0; i<item.length; i++)
                    {
                        var option = $("<option>").val(item[i].id).text(item[i].temItemName);
                        $("#"+name).append(option);
                        layui.form.render('select', 'sel_tem_item');
                    }
                }
            }
        }
    );
}




function GetAccount(house_id, loc){   //id号, 位置
    //TODO 查询账户余额
    $.ajax({
        type: "post",
        async: false,
        url: "/Manage/getManageAccount",
        data: {
            house_id:house_id
        },
        success:function (data) {
            if(data.status == 200){
                if(data.data == null){
                    $(loc).val(0).text(0)
                }else{
                    $(loc).val(data.data.account).text(data.data.account)
                }
            }
        }
    });
}


function renderCashback(){
    //TODO 加载返现金额
    layui.form.on('select(amount)', function () {
        $.ajax(
            {
                type: "post",
                async: false,
                url: "/Manage/listSysAmount",
                data: {
                    id:$("#amount_id").val(),
                    status:0
                },
                success:function (data) {
                    if(data.status == 200){
                        $('#cashback').val(data.data[0].cashback);
                        sumamount();
                    }
                }
            }
        );
    });
}



function renderPakingAreaPay(name){
    //TODO 加载默认车位区域金额
    layui.form.on('select(sel_pakingareapay)', function () {
        $.ajax(
            {
                type: "post",
                async: false,
                url: "/Manage2/getSysPakingarea",
                data: {
                    id:$("#pakingarea_id").val()
                },
                success:function (data) {
                    if(data.status == 200){
                        $(name).val(data.data.pakingareaPay);
                    }
                }
            }
        );
    });
}


function sumamount(){
    //TODO 计算总金额
    var sum = parseFloat($("#amount_id option:selected").text()) + parseFloat($('#cashback').val());
    $('#rechargeall').val(sum.toFixed(2));
}





function LoadDrug(){
    //TODO 加载药品信息
    var drugdata = '';
    $.ajax(
        {
            type: "get",
            async: false,
            url: "/drug-info/listByStatus",
            data:{
                status:'1'
            },
            success:function (data) {
                if(data.code == 200){
                    drugdata = data.data;
                }
            }
        }
    );
    return drugdata;
}
